package view.GUI.adminView.coreData;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import model.Configuration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.controlsfx.validation.ValidationSupport;

import view.GUI.mainView.MainView;
import view.Validator.MyValidationSupport;
import enums.GUIText;


public abstract class AdminDataManaging extends Application{
	protected final static Logger infoLog = LogManager.getLogger("INFO");
	protected final static Logger log = LogManager.getLogger("ERROR");

	protected Button btnSave;
	private Button btnCancel;
	
	private Label lblTitle;
	private Separator sepTop;
	private Separator sepBottom;

	private GridPane root;
	private ScrollPane sp;

	private int rowIndex;

	protected Stage primaryStage;
	private Scene scene;

	protected MainView owner;

	protected ValidationSupport valSup;
	
	private GUIText title;

	public AdminDataManaging(boolean isEdit, int rows, MainView instance, GUIText title) {
		this.rowIndex = rows;
		this.owner = instance;
		this.title = title;
		this.valSup = MyValidationSupport.createDefaultValidationSupport();
		createWidgets(title);
		addWidgets();
		setupLayout(isEdit);
		setupInteraction();
	}

	private void createWidgets(GUIText title) {

		btnSave = new Button(GUIText.SAVE.toString());
		btnCancel = new Button(GUIText.CANCEL.toString());

		root = new GridPane();
		sp = new ScrollPane();
		
		btnSave.setDisable(true);
		lblTitle = new Label(title.toString());
		sepBottom = new Separator();
		sepTop = new Separator();
	}

	private void addWidgets() {
		int endRow = rowIndex + 2;
		root.add(lblTitle, 0, 0);
		root.add(sepTop, 0, 1);
		root.add(btnSave, 0, endRow);
		root.add(btnCancel, 1, endRow);
		root.add(sepBottom, 0, ++endRow);

	}

	private void setupLayout(boolean isEdit) {
		if(isEdit){
			lblTitle.setId("edit");			
		} else{
			lblTitle.setId("new");
		}
		root.getStyleClass().add("grid");
		root.setAlignment(Pos.CENTER);
		GridPane.setColumnSpan(sepBottom, 5);
		GridPane.setColumnSpan(sepTop, 5);
		GridPane.setColumnSpan(lblTitle, 5);
		sp.setFitToHeight(true);
		sp.setFitToWidth(true);
	}

	private void setupInteraction() {
		btnCancel.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				primaryStage.close();
			}
		});
		btnSave.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				if(save()){
					primaryStage.close();					
				}
			}
			
		});
	}
	
	/**
	 * All subclasses need to override this method in order to define the actions for the save button.
	 * @return 
	 */
	protected abstract boolean save();
	
	protected void addNode(Node node, int column, int row){
		int actualrow = row + 2;
		root.add(node, column, actualrow);
	}
	
	

	@Override
	public void start(Stage primaryStage) throws Exception {
		sp.setContent(root);
		this.scene = new Scene(root); // Until ControlsFX Bug is fixed https://bitbucket.org/controlsfx/controlsfx/issue/285/validator-support-change-resize-behavior
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEVALIDATION).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEADMIN).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEROOT).toExternalForm());
		this.primaryStage = primaryStage;
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
		primaryStage.initOwner(owner.getPrimaryStage());
		primaryStage.setTitle(title.toString());
		primaryStage.setScene(scene);
		primaryStage.setResizable(true);
		primaryStage.centerOnScreen();
		primaryStage.show();
	}
}
